<?php
require_once(dirname(__FILE__).'/Constants.php');
require_once(dirname(__FILE__).'/Config.php');
require_once(dirname(__FILE__).'/Analytics.php'); //for google analytics

function is_date ($str) {
    if ($str === '') return TRUE;
    if (preg_match('/^\d{4}-\d{1,2}-\d{1,2}$/', $str)) {
        list($y, $m, $d) = explode('-', $str);
        return is_date_set($y, $m, $d);
    }
    else if (preg_match('/^\d{4}\/\d{1,2}\/\d{1,2}$/', $str)) {
        list($y, $m, $d) = explode('/', $str);
        return is_date_set($y, $m, $d);
    }
    else if (preg_match('/^\d{4}\d{2}\d{2}$/', $str)) {
        $y = substr($str, 0, 4);
        $m = substr($str, 4, 2);
        $d = substr($str, 6, 2);
        return is_date_set($y, $m, $d);
    }
    return FALSE;
}

function is_date_set ($year, $month, $day) {
    return
        ($year === '' && $month === '' && $day === '') ||
        ($year !== '' && $month !== '' && $day !== '' &&
        $year >= 1900 && $year <= 2100 &&
        checkdate($month, $day, $year));
}

function is_time ($str) {
    return TRUE;
}

function validate_signature ($params) {
    if (!isset($params['opensocial_owner_id']))
        return FALSE;
    if (!isset($params['opensocial_app_id']))
        return FALSE;
    return TRUE;
}

function make_mixi_url ($url) {
    if (IS_DEVEL)
        return $url;
    else
        return '?guid=ON&url='.urlencode(URL_PREFIX.$url);
}

function date_to_unix ($date) {
    if (!$date) return 0;
    list($y, $m, $d)   = explode('-', $date);
    return mktime(0, 0, 0, $m, $d, $y);
}

function time_to_sec ($time) {
    if (!$time) return 0;
    list($h, $m, $s) = explode(':', $time);
    return $h * 3600 + $m * 60 + $s;
}

function to_array ($var) {
    if (!isset($var)) return;
    if (!is_object($var)) return $var;
    $array = (array) $var;
    foreach ($array as &$elem)
        $elem = to_array($elem);
    return $array;
}

function days_in_month ($year, $month) {
    return date('d', mktime($year, $month + 1, -1, 0, 0, 0));
}
